from django.db import models
from django.contrib.auth.models import AbstractUser

from django.db import migrations


class UserInfo(AbstractUser):
    '''用户表'''
    telephone = models.CharField(verbose_name='手机号码', max_length=11)
    icon = models.ImageField(verbose_name='头像', upload_to='icon', default='icon/default.png')
    is_show = models.BooleanField(verbose_name='注销', default=False)

    def __str__(self):
        return self.username


class ChartRoom(models.Model):
    '''聊天室(三合一)'''
    chart_room_type = (
        (1, '无障碍聊天室'),
        (2, '密码保护群组'),
        (3, '一对一聊天群组')
    )
    name = models.CharField(verbose_name='聊天室名称', max_length=225, null=True, blank=True)

    room_group = models.BigIntegerField(verbose_name='聊天室组名', null=True, blank=True)  # none 表示无障碍聊天室
    chart_type = models.SmallIntegerField(verbose_name='聊天室类型', choices=chart_room_type, default=2)
    content = models.TextField(verbose_name='聊天室内容')
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    # 自动更新 , auto_now_add=True(相当于创建时间) auto_now=True(自动更新当前，修改一次改变一次)
    update_datetime = models.DateTimeField(verbose_name='更新时间', auto_now=True)

    userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.CASCADE, related_name='chart_room')
    channel_name = models.CharField(verbose_name='匿名的别名', max_length=225, default='mua')
    survival_time = models.BigIntegerField(verbose_name='聊天室存活时间', default=9923372036854775,
                                           help_text='默认永久存在，最小单位为1分钟')
    is_show = models.BooleanField(verbose_name='注销', default=False)
    is_star = models.BooleanField(verbose_name='星标', default=False)


class Group(models.Model):
    '''组名'''
    group = models.CharField(verbose_name='组名', max_length=225)
    userinfo = models.ForeignKey(verbose_name='创建的人', to='UserInfo', on_delete=models.DO_NOTHING)
    join_user = models.CharField(verbose_name='参与者的username', max_length=225, null=True, blank=True)


class KaidanRestaurant(models.Model):
    '''怪谈餐馆'''
    name = models.CharField(verbose_name='文章标题', max_length=64)
    dec = models.CharField(verbose_name='文章简介', max_length=225, null=True, blank=True)
    content = models.TextField(verbose_name='文章内容')

    userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING,
                                 related_name='kaidan_restaurant')

    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    start = models.BooleanField(verbose_name='星标', default=False)
    Step_up = models.IntegerField(verbose_name='点赞', default=0)
    Step_down = models.IntegerField(verbose_name='点踩', default=0)

    is_delete = models.BooleanField(verbose_name='注销', default=False)

    def __str__(self):
        return self.name


'''
创建文章的时候，创建一张文章点赞/点踩表--1


'''


class UpDownComment(models.Model):
    '''点赞点踩评论'''
    type_choices_comment = ((1, '怪谈文章'), (2, '螃蟹文章'), (3, '怪谈评论'), (4, '螃蟹评论'), (5, '不是评论'))
    type_choices_up_down = ((1, '怪谈文章点赞/踩'), (2, '螃蟹文章点赞/踩'), (3, '怪谈评论点赞/踩'), (4, '螃蟹评论点赞/踩'), (5, '不是点赞/踩'))

    type_comment = models.SmallIntegerField(verbose_name='评论类型', choices=type_choices_comment, default=5)
    type_up_down = models.SmallIntegerField(verbose_name='点赞/踩类型', choices=type_choices_comment, default=5)

    comment = models.CharField(verbose_name='评论内容', max_length=225, null=True, blank=True)
    kaidan_restaurant = models.ForeignKey(verbose_name='怪谈点赞点踩/评论', to='KaidanRestaurant', on_delete=models.DO_NOTHING,
                                          related_name='up_down_comment', null=True, blank=True)  #

    never_love_me = models.ForeignKey(verbose_name='螃蟹点赞点踩/评论', to='NeverLoveMe', on_delete=models.DO_NOTHING,
                                      related_name='up_down_cot', null=True, blank=True)  #

    parent = models.ForeignKey(verbose_name='评论', to='self', related_name='children', on_delete=models.CASCADE,
                               null=True, blank=True)
    userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING,
                                 related_name='up_down_userinfo', default=None)

    Step_up = models.IntegerField(verbose_name='点赞', default=0)
    Step_up_true = models.BooleanField(verbose_name='点赞确认', default=False)
    Step_down = models.IntegerField(verbose_name='点踩', default=0)
    Step_down_true = models.BooleanField(verbose_name='点踩确认', default=False)

    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    is_delete = models.BooleanField(verbose_name='注销', default=False)


class NeverLoveMe(models.Model):
    '''螃蟹在波我的壳'''
    name = models.CharField(verbose_name='文章标题', max_length=225)
    to_one = models.CharField(verbose_name='收信人', max_length=225, blank=True, null=True)
    content = models.TextField(verbose_name='文章内容')
    userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING,
                                 related_name='never_loveMe')

    Step_up = models.IntegerField(verbose_name='点赞', default=0)
    Step_down = models.IntegerField(verbose_name='点踩', default=0)

    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    start = models.BooleanField(verbose_name='星标', default=False)
    is_delete = models.BooleanField(verbose_name='注销', default=False)

    def __str__(self):
        return self.name


class PricePolicy(models.Model):
    """ 价格策略 """
    category_choices = (
        (1, 'v8'),
        (2, 'v12'),
        (3, 'v520'),
    )
    category = models.SmallIntegerField(verbose_name='收费类型', default=1, choices=category_choices)
    title = models.CharField(verbose_name='标题', max_length=64)
    price = models.PositiveIntegerField(verbose_name='价格')  # 0- 正整数
    # chart_num = models.PositiveIntegerField(verbose_name='聊天室个数')
    chat_permissions = models.SmallIntegerField(verbose_name='聊天权限', default=1)
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    def __str__(self):
        return self.title


class Transaction(models.Model):
    """ 交易记录 """
    status_choice = (
        (0, '未支付'),
        (1, '已支付')
    )
    status = models.SmallIntegerField(verbose_name='状态', choices=status_choice)

    order = models.CharField(verbose_name='订单号', max_length=64, unique=True)  # 唯一索引

    userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING,
                                 related_name='transaction')
    price_policy = models.ForeignKey(verbose_name='价格策略', to='PricePolicy', on_delete=models.DO_NOTHING,
                                     related_name='transaction')

    count = models.IntegerField(verbose_name='数量（年）', help_text='0表示无限期')

    price = models.IntegerField(verbose_name='实际支付价格')

    start_datetime = models.DateTimeField(verbose_name='开始时间', null=True, blank=True)
    end_datetime = models.DateTimeField(verbose_name='结束时间', null=True, blank=True)
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    def __str__(self):
        return self.order
# 气泡框(v520才有)
